import { ApiCall } from "tsrpc";
import { ReqRegister, ResRegister } from "../../../tsrpc/shared/protocols/account/PtlRegister";
import { User } from "../../../module/common/User";

export default async function (call: ApiCall<ReqRegister, ResRegister>) {
    if (call.req.account.length == 0) {
        call.error(`注册用户名不能为空`, { code: 'UserName_NoNull' });
        return;
    }

    // 创建用户表自增量唯一表示
    var query = await User.getUserByAccount(call.req.account);
    if (query == null) {
        // 插入用户数据
        var key = await User.addAccount(call.req.account, call.req.password);

        // 返回客户端结果
        call.succ({ uid:key });
    }
    else {
        call.error(`注册账号[${call.req.account}]已存在`, { code: 'Account_Exist' });
    }
}